﻿using Nomad.ASPMVVM;
using System;
using System.Web;
using System.Web.Security;

namespace ASP.MVVM.ViewModel {
    public class LoginViewModel : PageViewModel {
        public string Username {
            get { return GetValue<String>("Username"); }
            set { SetValue("Username", value); }
        }
        public string Password {
            get { return GetValue<String>("Password"); }
            set { SetValue("Password", value); }
        }
        public bool AlertVisible {
            get { return GetValue<Boolean>("AlertVisible"); }
            set { SetValue("AlertVisible", value); }
        }

        public ICommand Authenticate { get { return new BindingCommand(OnAuthenticate); } }

        public override void InitializeProperties() {
            this.Username = null;
            this.Password = null;
            this.AlertVisible = false;
        }

        private void OnAuthenticate() {
            var allowAccess = Models.User.Authenticate(this.Username, this.Password);
            if (allowAccess == false) {
                this.AlertVisible = true;
                this.Password = null;
            } else {
                FormsAuthentication.RedirectFromLoginPage(Username, false);
            }
        }
    }
}